<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
%>
<html>
	<head>
		<title>工余料库</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
	
		var lastIndex;
		$(function(){
			$("#dept_id").combotree({
				url:'<%=contextPath %>/springMVC/group/listDepartment',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					searchDepart();
				}
			});
			$("#warehouse").combogrid({
				title:'选择仓库',
				panelWidth:550,
				panelHeight:400,
				url:'<%=contextPath %>/springMVC/warehouse/listPageWareHouse?type=2',
				idField:"id",
				textField:"name",
				rownumbers:true,
				fitColumns:true,
				columns: [[
					{field:'id',hidden:true},
					{field:'type',hidden:true},
					{field:'dept_name',title:'所属部门',width:100,align:'center'},
					{field:'name',title:'仓库名称',width:100,align:'center'}
				]],
				pageSize:10,
				pageList:[10,50,100],  
				pagination:true,
				toolbar:'#tb'
			});
			
			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			$('#mtb').hide();
			
			$('#table').datagrid({
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'产品id',field:'product_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:150,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageMaterial',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'id',title:'主键',hidden:true},  
						       {field:'material_type_name',title:'产品类别',width:100,align:'center'},
						       {field:'code',title:'产品编号',width:100,align:'center'},
						       {field:'name',title:'产品名称',width:150,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'}
						    ]],
						    onBeforeLoad:function(param){
								var mSearchValue = $('#mSearchValue').val();
								var material_type_id = $('#material_type_id').combotree('getValue');
								param.searchValue=mSearchValue;
								param.material_type_id=material_type_id;
							},
						    toolbar:'#mtb',
						    pageList : [10,50,100],  
							pagination:true,
							onClickRow:function(rowIndex,rowData){
								if(checkSameProduct(rowData,$('#table').datagrid('getData').rows)){
									var pRow = $('#table').datagrid('getRows')[lastIndex];
									pRow.id = rowData.id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.format_type = rowData.format_type;
									$('#table').datagrid('getData').rows[lastIndex]=pRow;
									var data = $('#table').datagrid('getData');
									$('#table').datagrid('loadData',data);
									
									$('#table').datagrid('endEdit', lastIndex);
									$('#table').datagrid('beginEdit', lastIndex);
									
									//lastIndex =-1;
								}else{
									$.messager.alert("提示","选择的产品已经存在");
								}
							}
						}
					}},
					{title:'产品名称',field:'name',width:300,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:100,align:'center'},
					{title:'库存上限',hidden:true,field:'top',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'库存下限',hidden:true,field:'low',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'单价',field:'cost',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'库存数量',field:'num',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							myFlag:'1',
							backFunction:'addRow'
						}
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'top'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择产品',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							addRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
			
		});
		function searchDepart(){
			var dept_id =  $("#dept_id").combotree('getValue');
			$('#warehouse').combogrid('clear');
			$('#warehouse').combogrid("grid").datagrid('load',{
				dept_id:dept_id
			});
		}
		function domSearch()
		{
			var mSearchValue = $('#mSearchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				top:'1000',
				low:'0',
				cost:'0',
				num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addDB(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			
			var warehouse = $("#warehouse").combogrid("getValue");
			if(!checkNullOREmpty(warehouse,'仓库为必填项')){return;}
			var products="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(products!=""){
					products+=";";
				}
				products+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
					$.messager.alert('提示','第'+index+'行未选择产品！');
					return ;
				}else if(jsondata.rows[i].num == null || jsondata.rows[i].num == "" || jsondata.rows[i].num <= 0){
					$.messager.alert('提示','第'+index+'行数量填写不正确！');
					return ;
				}
				else if(jsondata.rows[i].cost == null || jsondata.rows[i].cost == "" || jsondata.rows[i].cost <= 0){
					$.messager.alert('提示','第'+index+'行价格填写不正确！');
					return ;
				}
				else{
					//判断该产品在该仓库是否存在，如果存在给出提示。避免重复添加
					var hasExist = 0;
					$.ajax({
						type:'POST',
						async:false,
						url:'<%=contextPath %>/springMVC/warehouse/checkSame',
						data:'warehouse_id='+warehouse+'&product_id='+jsondata.rows[i].id,
						success:function(rtJson){
							if(rtJson.rtState != '0'){
								$.messager.alert('提示','第'+index+'行产品库存已经存在,不能重复添加！');
								hasExist = 1;
							}
						}
					});
					if(hasExist == 1){
						return;
					}
				}
			}
			if(!checkNullOREmpty(products,'产品为必填项')){return;}
			
			var param = "warehouse_id="+warehouse+"&products="+products;
			//$(obj).hide();
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/warehouse/addMyMaterialDB',
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}
	</script>
	</head>
	<body>
		<div id="tb" style="padding:3px;height:auto">
			<div align="right">
				所属部门：<input  id="dept_id" name="dept_id" class="easyui-combotree">
			</div>
		</div>
		
		<div style="width: 90%;padding-left:50px">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>选择仓库：</td>
					<td class="td2" nowrap>
						<input  id="warehouse" name="warehouse" >
					</td>
					<td  nowrap>
						<div align="center">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addDB(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="3"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div style="float:left;">
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addOrEditMaterial(0);">新增材料</a>
				</div>
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
		
	</body>
</html>